package com.example.blog.service.impl;

import com.example.blog.dao.FavoriteDao;
import com.example.blog.dto.ArticleDTO;
import com.example.blog.dto.FavoriteDTO;
import com.example.blog.pojo.Favorite;
import com.example.blog.pojo.PageBean;
import com.example.blog.service.FavoriteService;
import com.example.blog.utils.ThreadLocalUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class FavoriteServiceImpl implements FavoriteService {

    @Autowired
    private FavoriteDao favoriteDao;

    @Override
    public List<FavoriteDTO> getFavoriteList(Integer id) {

        List<FavoriteDTO> favoritelist = favoriteDao.list(id);
        return favoritelist;

    }

    @Override
    public boolean deleteFavorite(Integer id, Integer userid) {

        Integer number = favoriteDao.deleteFavorite(id,userid);
        if (number > 0){
            return true;
        }
        else{
            return false;
        }

    }

    @Override
    public boolean addFavorite(Integer articleid) {
        //获取当前用户id
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userid = (Integer) map.get("id");

        //用户已经收藏过了,收藏失败
        Favorite favorite = favoriteDao.findFavorite(userid, articleid);
        if(favorite != null)return false;

        Integer n = favoriteDao.addFavorite(userid,articleid, LocalDateTime.now());
        if(n > 0)return true;
        else return false;

    }
}
